package com.qzsoft.zhjd.materiel.modules.materielhome.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.zhjd.common.utils.ResultUtils;
import com.qzsoft.zhjd.materiel.modules.materielbatch.dto.MaterielBatchChartDto;
import com.qzsoft.zhjd.materiel.modules.materielbatch.service.MaterielBatchService;
import com.qzsoft.zhjd.materiel.modules.materieldata.dto.MaterielDataListDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.enmus.WeightTypeEnum;
import com.qzsoft.zhjd.materiel.modules.materieldata.service.MaterielDataService;
import com.qzsoft.zhjd.materiel.modules.materielhome.dto.MaterielHomeConsumeDto;
import com.qzsoft.zhjd.materiel.modules.materielhome.dto.MaterielReceiveSendChartDto;
import com.qzsoft.zhjd.materiel.modules.materielhome.dto.MaterielReceiveSendCountDto;
import com.qzsoft.zhjd.materiel.modules.materielhome.service.MaterielHomeService;

/**
 * @ClassName MaterielHomeController
 * @Description 首页数据
 * @author linhuawei
 * @since 2022-04-05
 */
@RestController
@RequestMapping("/api/materielHome")
public class MaterielHomeController {

    @Autowired
    private MaterielDataService materielDataService;

    @Autowired
    private MaterielBatchService materielBatchService;

    @Autowired
    private MaterielHomeService materielHomeService;

    /**
     * @Title getReceiveCount
     * @Description 收料统计分析
     * @param baseCode 基地编号
     * @return 收料统计分析
     */
    @GetMapping("/getReceiveCount")
    public Result<Map<String, MaterielReceiveSendCountDto>> getReceiveCount(
            @RequestParam(required = true) String baseCode) {
        Map<String, MaterielReceiveSendCountDto> data = materielHomeService.getReciveSendCount(baseCode,
                WeightTypeEnum.RECEIVE);
        return ResultUtils.ok(data);
    }

    /**
     * @Title getReceiveChart
     * @Description 收料统计图表
     * @param baseCode 基地编号
     * @param type     图表类型，本日、本月、本年
     * @return 收料统计图表
     */
    @GetMapping("/getReceiveChart")
    public Result<MaterielReceiveSendChartDto> getReceiveChart(@RequestParam(required = true) String baseCode,
            @RequestParam(required = false, defaultValue = "year") String type) {
        MaterielReceiveSendChartDto data = materielHomeService.getReceiveSendChart(baseCode, type,
                WeightTypeEnum.RECEIVE);
        return ResultUtils.ok(data);
    }

    /**
     * @Title getSendCount
     * @Description 发料统计分析
     * @param baseCode 基地编号
     * @return 发料统计分析
     */
    @GetMapping("/getSendCount")
    public Result<Map<String, MaterielReceiveSendCountDto>> getSendCount(
            @RequestParam(required = true) String baseCode) {
        Map<String, MaterielReceiveSendCountDto> data = materielHomeService.getReciveSendCount(baseCode,
                WeightTypeEnum.SEND);
        return ResultUtils.ok(data);
    }

    /**
     * @Title getSendChart
     * @Description 发料统计图表
     * @param baseCode 基地编号
     * @param type     图表类型，本日、本月、本年
     * @return 发料统计图表
     */
    @GetMapping("/getSendChart")
    public Result<MaterielReceiveSendChartDto> getSendChart(@RequestParam(required = true) String baseCode,
            @RequestParam(required = false, defaultValue = "year") String type) {
        MaterielReceiveSendChartDto data = materielHomeService.getReceiveSendChart(baseCode, type, WeightTypeEnum.SEND);
        return ResultUtils.ok(data);
    }

    /**
     * @Title getRealtime
     * @Description 获取实时数据
     * @param params
     * @return
     */
    @GetMapping("/getRealtime")
    public Result<List<MaterielDataListDto>> getRealtime(@RequestParam(required = true) String baseCode) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("baseCode", baseCode);
        params.put("current", 1);
        params.put("size", 100);
        IPage<MaterielDataListDto> page = materielDataService.pageDto(params);
        return ResultUtils.ok(page.getRecords());
    }

    /**
     * @Title getBatchChart
     * @Description 获取过磅车次图表数据
     * @param baseCode 基地主键
     * @param year     年份
     * @return 过磅车次图表数据
     */
    @GetMapping("/getBatchChart")
    public Result<List<MaterielBatchChartDto>> getBatchChart(@RequestParam(required = true) String baseCode,
            @RequestParam(required = true) String year) {
        List<MaterielBatchChartDto> list = materielBatchService.getChartForHome(baseCode, year);
        return ResultUtils.ok(list);
    }

    /**
     * @Title getConsume
     * @Description 材料消耗
     * @param baseCode
     * @param year
     * @return
     */
    @GetMapping("/getConsume")
    public Result<Map<String, List<MaterielHomeConsumeDto>>> getConsume(@RequestParam(required = true) String baseCode,
            @RequestParam(required = true) String year) {
        Map<String, List<MaterielHomeConsumeDto>> data = materielHomeService.getConsume(baseCode, year);
        return ResultUtils.ok(data);
    }

}
